/**
 * 文  件  名：  ThirdAccountDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-13
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IThirdAccountDao;
import com.qiyou.entity.ThirdAccount;
import com.qiyou.entity.UserInfo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 三方账号数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class ThirdAccountDaoImpl implements IThirdAccountDao {
    private Connection conn = null;

    public ThirdAccountDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(ThirdAccount thirdAccount) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_third_account`(`userID`, `taID`, `taType`) values(?, ?, ?)";

        Object[] values = TransformUtil.beanTOArray(thirdAccount, false);

        result = DBUtil.execute(conn, sql, values);
        return result;
    }

    @Override
    public boolean doDelete(ThirdAccount thirdAccount) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_third_account` where `userID` = ? and `taID` = ? and `taType` = ?";
        Object[] params = TransformUtil.beanTOArray(thirdAccount, false);
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(ThirdAccount thirdAccount) throws SQLException {
        boolean result = false;
        String sql = "update `tb_third_account` set `taID` = ? where `userID` = ? and `taType` = ?";

        Object[] params = TransformUtil.beanTOArray(thirdAccount, false);
        result = DBUtil.execute(conn, sql, params[1], params[0], params[2]);
        return result;
    }

    @Override
    public List<ThirdAccount> getThirdAccount(String userID)
            throws SQLException {
        List<ThirdAccount> result = null;
        String sql = "select * from `tb_third_account` where `userID` = ?";

        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<ThirdAccount>(all.size());
            ThirdAccount thirdaccont = null;
            for (Object[] objects : all) {
                thirdaccont = new ThirdAccount();
                thirdaccont = (ThirdAccount) TransformUtil.arrayToBean(objects,
                        ThirdAccount.class);
                result.add(thirdaccont);
            }
        }
        return result;
    }

    @Override
    public List<UserInfo> getThirdAccount(Object... params) throws SQLException {
        List<UserInfo> result = null;
        StringBuffer sql = new StringBuffer(
                "select `tb_user_info`.* from `tb_user_info`, `tb_third_account` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            result = new ArrayList<UserInfo>(all.size());

            for (Object[] objects : all) {
                UserInfo userinfo = (UserInfo) TransformUtil.arrayToBean(
                        objects, UserInfo.class);
                result.add(userinfo);
            }
        }

        return result;
    }

}
